#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2024 SteamFork (https://github.com/SteamFork)

if [ -f "${HOME}/.SteamFork/options" ]; then
	source "${HOME}/.SteamFork/options"
fi

for KEY in WWWHOST WWWUSER WWWROOT SIGNKEY REPO_DIR IMAGE_DIR MIRROR_HOSTS MIRROR_KEY; do
	if [ -z "${!KEY}" ]; then
		echo "Export ${KEY} before using this script."
		exit 1
	fi
done

function private_key() {
	case ${1} in
	add)
		echo "Copy private key to ${WWWHOST}."
		scp ${HOME}/.SteamFork/${MIRROR_KEY} ${WWWUSER}@${WWWHOST}:.ssh
		ssh -tt ${WWWUSER}@${WWWHOST} "chmod 0700 .ssh; chmod 0600 .ssh/${MIRROR_KEY}"
		;;
	remove)
		echo "Remove private key from ${WWWHOST}"
		ssh -tt ${WWWUSER}@${WWWHOST} "rm -f .ssh/${MIRROR_KEY}"
		;;
	esac
}

### Generate a mirror list.
echo "www.steamfork.org" >/tmp/mirrorlist.txt
for HOST in ${WEB_HOSTS[*]#*@}; do
	echo ${HOST} >>/tmp/mirrorlist.txt
done

case ${1} in
pkgs | repo)
	echo "Syncing repository to ${WWWHOST}."
	rsync -av --delete --progress --stats ${REPO_DIR} ${WWWUSER}@${WWWHOST}:${WWWROOT}/
	ssh -tt ${WWWUSER}@${WWWHOST} "chown -R www-data:www-data ${WWWROOT}/"
	;;
os-sync)
	echo "Remove stale image directories on ${WWWHOST}."
	ssh -tt ${WWWUSER}@${WWWHOST} "cd ${WWWROOT}/; rm -rf images.new ||:; cp -rf images images.new"
	for REL in rel minimal; do
		DEPLOY_DIR="${IMAGE_DIR}/${REL}/images"
		if [ -e "${DEPLOY_DIR}/latest_${REL}.releasemeta" ]; then
			echo "Syncing ${REL} to ${WWWHOST}."
			source ${DEPLOY_DIR}/latest_${REL}.releasemeta
			UPDATEFILE=${IMAGEFILE}
			IMAGEFILE="$(echo ${IMAGEFILE} | sed 's~_~-~g')"
			ssh -tt ${WWWUSER}@${WWWHOST} "cd ${WWWROOT}/images.new; rm -f \$(ls | grep steamfork_${REL}_ | grep -v ${STAGING_OS_TAG}) latest_${REL}*"
			ssh -tt ${WWWUSER}@${WWWHOST} "cd ${WWWROOT}/images.new/installer; rm -f \$(ls | grep steamfork-${REL}- | grep -v ${STAGING_OS_TAG}) latest_${REL}*"
			while true; do
				rsync -av --progress --stats ${DEPLOY_DIR}/* ${WWWUSER}@${WWWHOST}:${WWWROOT}/images.new/
				if [ $? = 0 ]; then
					break
				fi
				sleep 5
			done
			ssh -tt ${WWWUSER}@${WWWHOST} "cd ${WWWROOT}/images.new/installer/; sha256sum ${IMAGEFILE}-x86_64.iso | tee -a ${IMAGEFILE}-x86_64.iso.sha256"
			ssh -tt ${WWWUSER}@${WWWHOST} "cd ${WWWROOT}/images.new/installer/; ln -sf ${IMAGEFILE}-x86_64.iso steamfork-${REL}-latest-x86_64.iso"
			ssh -tt ${WWWUSER}@${WWWHOST} "cd ${WWWROOT}/images.new/installer/; ln -sf ${IMAGEFILE}-x86_64.iso.sha256 steamfork-${REL}-latest-x86_64.iso.sha256"
		fi
	done
	scp /tmp/mirrorlist.txt ${WWWUSER}@${WWWHOST}:${WWWROOT}/
	ssh -tt ${WWWUSER}@${WWWHOST} "mkdir -p ${WWWROOT}/meta/steamos/amd64/${STAGING_OS_TAG}.0/steamdeck; echo "{ }" >${WWWROOT}/meta/steamos/amd64/${STAGING_OS_TAG}.0/steamdeck/${STAGING_OS_TAG}.json"

	;;
os-release)
	private_key add
	for HOST in ${WWWUSER}@${WWWHOST} ${MIRROR_HOSTS}; do
		echo "Swap to release images on ${HOST}."
		ssh -tt ${HOST} "sudo chown -R www-data:www-data ${WWWROOT}/images.new"
		ssh -tt ${HOST} "sudo rm -rf ${WWWROOT}/images.prev; sudo mv ${WWWROOT}/images ${WWWROOT}/images.prev; sudo mv ${WWWROOT}/images.new/ ${WWWROOT}/images"
	done
	private_key remove
	;;
mirrors)
	private_key add
	echo "Push mirror list to ${WWWHOST}."
	scp /tmp/mirrorlist.txt ${WWWUSER}@${WWWHOST}:${WWWROOT}/
	(
		for MIRROR_HOST in ${MIRROR_HOSTS}; do
			echo "Sync ${MIRROR_HOST} from ${WWWUSER}@${WWWHOST}"
			ssh -tt ${MIRROR_HOST} "cd ${WWWROOT}; sudo rsync -av --delete --progress --stats rsync://${WWWHOST}/steamfork/ ." &
		done
		wait
	)
	private_key remove
	;;
check)
	sudo pacman -Sy >/dev/null 2>&1
	REPO_DIR="${REPO_DIR}/${STEAMOS_VERSION}/os/${OS_ARCH}"
	for REPO_PACKAGE in ${BUILD_DIR}/PKGBUILD/*; do
		source ${REPO_PACKAGE}/PKGBUILD
		for TEST_PACKAGE in ${pkgname[@]}; do
			EXISTS=$(pacman -Ss ${TEST_PACKAGE} 2>&1 | grep "${TEST_PACKAGE} ${pkgver}-${pkgrel}")
			if [ -z "${EXISTS}" ]; then
				echo "Error: ${TEST_PACKAGE} ${pkgver}-${pkgrel} is not in the repo."
				exit 1
			fi
			unset EXISTS pkgname
		done
	done
	;;
esac

rm -f /tmp/mirrorlist.txt
